package com.design.testdemo.activity;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;

import com.design.testdemo.R;

public class RunTimeActivity extends AppCompatActivity implements View.OnClickListener {

    private TextView mTvTime;
    private RunTimeUtils mRunTimeUtils;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_run_time);

        mTvTime = (TextView) findViewById(R.id.tv_time);

        mRunTimeUtils = RunTimeUtils.getInstance();
        mRunTimeUtils.setTimeListener(new RunTimeUtils.TimeListener() {
            @Override
            public void onTimeChange(long timer) {

                mTvTime.setText(getFormatTime(timer));

            }
        });
        mTvTime.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        mRunTimeUtils.initTime();
    }

    private String getFormatTime(long time) {

        long millisecond = time % 1000;
        long second = (time / 1000) % 60;
        long minute = time / 1000 / 60;

        //秒以下的只显示一位
        String strMillisecond = millisecond / 10 + "";

        if (strMillisecond.length() == 1) {
            strMillisecond = "0" + strMillisecond;
        }

        //秒显示两位
        String strSecond = ("00" + second).substring(("00" + second).length() - 2);
        //分显示两位
        String strMinute = ("00" + minute).substring(("00" + minute).length() - 2);

        return strMinute + ":" + strSecond + "." + strMillisecond;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mRunTimeUtils.stopTimer();

    }
}
